package com.lagou.edu.service.impl;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.controller.dao.LagouAuthCodeDao;
import com.lagou.edu.enums.CodeStatus;
import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.service.EmailFeignClient;
import com.lagou.edu.service.CodeService;
import com.lagou.edu.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zjy
 */
@Service
@Slf4j
public class CodeServiceImpl implements CodeService {



    @Autowired
    EmailFeignClient emailFeignClient;

    @Autowired
    LagouAuthCodeDao lagouAuthCodeDao;

    @Value("${code.expire}")
    Integer expire;


    @Override
    public R generateCode(String emailAddress) {
        String code = String.valueOf((int)((Math.random()*9+1)*100000));
        List<LagouAuthCode> lagouAuthCodeList = lagouAuthCodeDao.findLagouAuthCodeByEmailOrderByExpiretimeDesc(emailAddress);
        if(!CollectionUtils.isEmpty(lagouAuthCodeList)){
            LagouAuthCode lagouAuthCode = lagouAuthCodeList.get(0);
            System.out.println(JSON.toJSON(lagouAuthCode));
            if(lagouAuthCode.getExpiretime().getTime() - System.currentTimeMillis() > 0){
                code = lagouAuthCode.getCode();
            }
        }
        Boolean sendEmailFlag = emailFeignClient.sendEmail(emailAddress, String.valueOf(code));
        if(sendEmailFlag){
            LagouAuthCode authCode = new LagouAuthCode();
            authCode.setCode(code);
            authCode.setEmail(emailAddress);
            authCode.setCreatetime(new Date());
            Calendar nowTime = Calendar.getInstance();
            nowTime.add(Calendar.MINUTE, expire);
            authCode.setExpiretime(nowTime.getTime());
            lagouAuthCodeDao.save(authCode);
            return R.ok(code);
        }else{
            return R.error("验证码发送失败，请检查邮箱地址稍后再试");
        }
    }

    @Override
    public Integer validateCode(String emailAddress, String code) {
        List<LagouAuthCode> list = lagouAuthCodeDao.findLagouAuthCodeByEmailOrderByExpiretimeDesc(emailAddress);
        if(CollectionUtils.isEmpty(list)){
            log.error("该邮箱地址不正确或未验证过>>>>{},{}",emailAddress,code);
            return CodeStatus.OTHER.getCode();
        }
        List<LagouAuthCode> lagouAuthCodeList = lagouAuthCodeDao.findLagouAuthCodeByEmailAndCodeOrderByExpiretimeDesc(emailAddress, code);
        //校验验证码是否正确，0正确1错误2超时
        if(CollectionUtils.isEmpty(lagouAuthCodeList)){
            log.error("验证码或邮箱不正确，请重新输入>>>>{},{}",emailAddress,code);
            return CodeStatus.WRONG.getCode();
        }
        //只选择最新的一个
        LagouAuthCode lagouAuthCode = lagouAuthCodeList.get(0);
        Long nowTime = System.currentTimeMillis();
        if(nowTime - lagouAuthCode.getExpiretime().getTime() >= 0){
            return CodeStatus.TIME_OUT.getCode();
        }
        return CodeStatus.RIGHT.getCode();
    }
}
